package com.yanqu.road.server.cmd.activity.unionchest.cross;

import com.yanqu.road.entity.activity.unionChest.config.UnionChestBoxConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.entity.log.LogUnionChest;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.UnionChestProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.unionchest.UnionChestModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.unionchest.UnionChestMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_UNIONCHEST_BOX_SHARE_CROSS_BACK, desc = "分享宝箱,跨服返回")
public class CrossUnionChestShareBackCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        UnionChestProto.CrossUnionChestShareBoxRespMsg respMsg = UnionChestProto.CrossUnionChestShareBoxRespMsg.parseFrom(packet.getBytes());
        ret = respMsg.getRet();
        UnionChestProto.UnionChestBoxShareBoxRespMsg.Builder builder = UnionChestProto.UnionChestBoxShareBoxRespMsg.newBuilder();
        builder.setActivityId(respMsg.getActivityId());
        builder.setRet(respMsg.getRet());
        if (respMsg.hasBox()) {
            builder.setBox(respMsg.getBox());
        }
        UnionChestBoxConfig boxConfig = UnionChestMgr.getConfig().getBoxMap().get(respMsg.getBox().getBoxType());
        Property cost = new Property(boxConfig.getBoxId(), 1);
        player.getModule(PreReduceModule.class).restorePreReduce(cost);

        player.sendPacket(ClientProtocol.U_UNIONCHEST_BOX_SHARE, builder);

        if (0 == ret) {//广播给商会成员

            player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.UnionChest, eLogMoneyType.UnionChestShare);


            UnionChestProto.UnionChestBoxSyncMsg.Builder syncMsg = UnionChestProto.UnionChestBoxSyncMsg.newBuilder();
            syncMsg.setActivityId(respMsg.getActivityId());
            syncMsg.addBoxList(respMsg.getBox());
            UnionChestProto.CrossUnionChestBoxSyncMsg.Builder crossMsg = UnionChestProto.CrossUnionChestBoxSyncMsg.newBuilder();
            crossMsg.setUnionId(respMsg.getUnionId());
            crossMsg.setSyncMsg(syncMsg);
            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C3_UNIONCHEST_BOX_SYNC, crossMsg);
            GamePlayerMgr.sendPacket(respMsg.getUserId(), message);

            WorldMessageProto.WorldMessageTempMsg.Builder chatMsg = WorldMessageProto.WorldMessageTempMsg.newBuilder();
            chatMsg.setType(eMessageType.UnionChest.getValue());
            chatMsg.setSendTime(DateHelper.getCurrentSecond());
            chatMsg.setUserId(respMsg.getUserId());
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(respMsg.getUserId(), GameServer.getInstance().getServerId());
            if (null != userBaseInfo) {
                chatMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }
            //宝箱类型|宝箱道具id
            String params = respMsg.getBox().getBoxType() + "|" + boxConfig.getBoxId();
            chatMsg.setExtraParams(params);

            WorldMessageMgr.sendUnionChat(respMsg.getUnionId(), chatMsg);

            AutoLogMgr.add(new LogUnionChest(respMsg.getActivityId(), respMsg.getUserId(), respMsg.getUnionId(),
                    respMsg.getBox().getBoxId(),
                    respMsg.getBox().getBoxType(),
                    respMsg.getBoxGoodsId(),
                    0,
                    respMsg.getBox().getShareUserId(),
                    "",
                    new Date(respMsg.getBox().getShareTime())
            ));
        }
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
